package com.yonyou.crm.bpub.initData.service;

import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yonyou.crm.bpub.initData.entity.InitDatasBodyVO;
import com.yonyou.crm.bpub.initData.entity.InitDatasItemVO;
import com.yonyou.crm.bpub.initData.entity.InitDatasListItemVO;
import com.yonyou.crm.bpub.initData.entity.InitDatasVO;
import com.yonyou.crm.common.exception.CrmBusinessException;
import com.yonyou.crm.cum.contact.entity.ContactVO;
import com.yonyou.crm.cum.cumprmt.entity.CumprmtVO;
import com.yonyou.crm.cum.cumprmt.service.CumprmtBizServiceImpl;

@Service
public class CumprmtInitDatas extends BaseInitDatas{
	
	@Autowired
	CumprmtBizServiceImpl service;
	
	@Override
	public <T extends Serializable> T getDetail(long id) {
		return (T)service.selectById(id);
	}
	
	@Override
	public ArrayList<InitDatasListItemVO> getHeader(InitDatasVO initData) {
		ArrayList<InitDatasListItemVO> headerList = new ArrayList<InitDatasListItemVO>();
		
		return headerList;
	}

	@Override
	public ArrayList<InitDatasBodyVO> getBody(InitDatasVO initData) {
		ArrayList<InitDatasBodyVO> body = new ArrayList<InitDatasBodyVO>();
		return body;
	}

	@Override
	public ArrayList<InitDatasBodyVO> getBodyDefault() {
		ArrayList<InitDatasBodyVO> bodyDefault = new ArrayList<InitDatasBodyVO>();
		return bodyDefault;
	}
	
	/**
	 * 获取模板配置(无)
	 */
	@Override
	public <T extends Serializable> InitDatasVO getLayout(T obj){
		CumprmtVO vo = (CumprmtVO) obj;
//		Long biztypeId = vo.getBiztypeId();
		// TODO...
		long biztypeId = 11;
		return layoutService.getInitDatasForMobile(biztypeId);
	}
	
	@Override
	public <T extends Serializable> ArrayList<InitDatasListItemVO> getHeader(T obj, InitDatasVO initData) {
		ArrayList<InitDatasListItemVO> headerList = new ArrayList<InitDatasListItemVO>();
		
		CumprmtVO vo = (CumprmtVO)obj;
		
		String[] itemkeyList = {"id","commitTime","orgName","deptName","applyUserName","cumName","cumFullname","approvalStateName","approvalUserName","approvalTime","approvalComment","description"};
		for (int i = 0; i< itemkeyList.length; i++) {
			InitDatasListItemVO headerItem = new InitDatasListItemVO();
			headerItem.setItemkey(itemkeyList[i]);
			ArrayList<InitDatasItemVO> itemdatalist = new ArrayList<>();
			InitDatasItemVO itemdata = new InitDatasItemVO();
			String dataid = "";
			String datavalue = "";
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			
			if (itemkeyList[i].equals("commitTime")) {
				dataid = datavalue =(vo.getCommitTime() == null) ? "" : sdf.format(vo.getCommitTime());
			} else if (itemkeyList[i].equals("approvalTime")) {
				dataid = datavalue =(vo.getApprovalTime() == null) ? "" : sdf.format(vo.getApprovalTime());
			} else {
				char[] cs = itemkeyList[i].toCharArray();
				cs[0] = Character.toUpperCase(cs[0]);
				String funcName = String.valueOf(cs);
				try {
					Object data = vo.getClass().getMethod("get"+funcName, new Class[]{}).invoke(vo, new Object[]{});
					dataid = (data == null) ? "" : data.toString();
					datavalue = dataid;
				} catch (IllegalAccessException
						| IllegalArgumentException
						| InvocationTargetException
						| NoSuchMethodException | SecurityException e) {
					throw new CrmBusinessException("动态调用cumprmt方法错误！");
				}
			}

			itemdata.setDataid(dataid);
			itemdata.setDatavalue(datavalue);
			itemdatalist.add(itemdata);
			headerItem.setItemdatalist(itemdatalist);
			headerList.add(headerItem);
			
		}
		return headerList;
	}

	@Override
	public <T extends Serializable> ArrayList<InitDatasBodyVO> getBody(T obj, InitDatasVO initData) {
		ArrayList<InitDatasBodyVO> body = new ArrayList<InitDatasBodyVO>();
		return body;
	}
	
}
